package com.monkeyboy.service;

import com.monkeyboy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @Author Gavin
 * @date 2021.01.22 15:13
 */
@Service
public class MongodbTestTransactional {
    @Autowired
    private MongoTemplate mongoTemplate;

    //添加了spring的事务后遇到异常就会回滚
    @Transactional//mongodb事务只支持副本集，这儿代表mysql的事务
    public void testInsertTransaction() {
        User user = new User();
        user.setId(1L);
        user.setUsername("test");
        user.setPassword("123456");
        user.setEmail("2294625664@qq.com");
        user.setTags(Arrays.asList("java", "python"));
        mongoTemplate.insert(user);
        int i=1/0;//此时的事务对mongodb不起作用
    }
}
